package com.atguigu.gulimall.ware.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.Exchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;

@Configuration
public class StockMQConfig {


    //交换机
    @Bean
    public Exchange stockEventExchange(){
        return new TopicExchange("stock-event-exchange", true, false);
    }

    //队列
    @Bean
    public Queue stockReleaseQueue(){
        Queue queue = new Queue("stock.release.queue", true, false, false);
        return queue;
    }

    //延时队列
    @Bean
    public Queue stockDelayQueue(){
        //自定义参数
        HashMap<String, Object> arguments = new HashMap<>();
        arguments.put("x-message-ttl", 6000);//有效期（单位：毫秒），到期未被签收就变成死信
        arguments.put("x-dead-letter-exchange", "stock-event-exchange");//消息变成死信后发送到指定的交换机
        arguments.put("x-dead-letter-routing-key", "stock.release");//死信发送到交换机的路由键

        Queue queue = new Queue("stock.delay.queue", true, false, false, arguments);
        return queue;
    }

    //绑定关系
    @Bean
    public Binding stockCreateBinding(){
        return new Binding("stock.delay.queue", Binding.DestinationType.QUEUE,"stock-event-exchange","stock.create",null);
    }

    //绑定关系
    @Bean
    public Binding orderReleaseBinding(){
        return new Binding("stock.release.queue", Binding.DestinationType.QUEUE,"stock-event-exchange","stock.release",null);
    }

}
